namespace AltNetUK.Models
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    using System.Xml.Serialization;
    using SimpleWebServices.Data;

    [XmlRoot(ElementName = "availability")]
    public sealed class Availability : IPersistable
    {
        [XmlIgnore]
        string IPersistable.Entity
        {
            get
            {
                StringBuilder buffer = new StringBuilder();
                buffer.Append(this.Date);
                buffer.Append('\0');
                buffer.Append(this.Key);
                buffer.Append('\0');
                buffer.Append(this.Total);

                return buffer.ToString();
            }
        }

        [XmlIgnore]
        Uri IPersistable.Identifier
        {
            get;
            set;
        }

        [XmlIgnore]
        Guid? IPersistable.Unique
        {
            get;
            set;
        }

        [XmlAttribute(AttributeName = "date")]
        public string Date
        {
            get;
            set;
        }

        [XmlAttribute(AttributeName = "key")]
        public string Key
        {
            get;
            set;
        }

        [XmlText]
        public int Total
        {
            get;
            set;
        }

        public static Availability Load(string key)
        {
            return Repository.Query<Availability>(string.Format(CultureInfo.InvariantCulture, "/availability[@key=\"{0}\"][1]", key))[0];
        }
    }
}